app.controller('userController', ['$scope', 'httpWrapService', 'dialogs', '$state', '$stateParams','GridFactory',
        function($scope, httpWrapService, dialogs, $state, $stateParams,GridFactory) {
    $scope.isAdmin = $stateParams.isAdmin;

    var columnDefs = [
        {name: "用户名", field: "name"},
        {name: "联系电话", field: "phone"} ,
        {name: "过期时间", field: "expiredDt", cellFilter : 'date : "yyyy-MM-dd"'} ,
        {name: "创建时间", field: "createDt"} ,
        {name: "最后登录时间", field: "flushDt"} ,
        {name: "是否未锁定", field: "nonLock", cellFilter: 'dictionaryFiter : "yesOrNo"'} ,
        {name: "是否启用", field: "enabled", cellFilter: 'dictionaryFiter : "yesOrNo"'} ,
        {
            displayName: "",
            field: "id",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-default m-b-xs w-xs" ng-click="grid.appScope.view(this)">查看/编辑</button>'
        },
        {
            displayName: "",
            field: "id",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-danger m-b-xs w-xs" ng-disabled="grid.appScope.Grid.deletes[\'{{COL_FIELD}}\']" ng-click="grid.appScope.delete(this)">删除</button>'
        }
    ];

    $scope.Grid = GridFactory.newInstance({
        url : "system/user/list/"+ $scope.isAdmin + ".do",
        columnDefs : columnDefs
    });
    $scope.Grid.load();


    $scope.add = function() {
        $state.go('app.system.user.view', {type : "add", isAdmin : $scope.isAdmin});
    };

    $scope.view = function(target) {
        $state.go('app.system.user.view', {id : target.$parent.row.entity.id, type:"update", isAdmin : $scope.isAdmin });
    };


    $scope.delete = function(target) {
        var id = target.$parent.row.entity.id;
        httpWrapService.defaultGridDelete($scope.Grid, id,'system/user/delete/'+ id +'.do').then(function(res){
            $scope.Grid.deleteRow(target.$parent.row.entity);
        });
    };

}]);